package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.service.AttrService;
import com.atguigu.gmall.pms.vo.AttrGroupVo;
import com.atguigu.gmall.pms.vo.ItemAttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }
    @Autowired
    AttrService attrService;
    @Override
    public List<AttrGroupVo>  queryAttrGroupAndAttrsByCategoryId(Long categoryId) {
        //查询SPU所属分类的 属性组集合  AttrGroup
        List<AttrGroupEntity> attrGroupEntities = this.list(new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCategoryId, categoryId));
        List<AttrGroupVo> attrGroupVos = attrGroupEntities.stream().map(attrGroupEntity -> {
            //每个attrGroupEntity对象 转为一个AttrGroupVo并且返回
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(attrGroupEntity , attrGroupVo);
            attrGroupVo.setAttrEntities(attrService.list(new LambdaQueryWrapper<AttrEntity>()
                    .eq(AttrEntity::getGroupId,attrGroupEntity.getId()).eq(AttrEntity::getType,1)));
            return attrGroupVo;
        }).collect(Collectors.toList());
        return attrGroupVos;
        //使用streamAPI优化：
//        List<AttrGroupVo> attrGroupVos = new ArrayList<>();
//        //每个属性分组 都有多个属性组成：遍历属性分组集合  为每个分组查找他的属性集合  Attr
//        attrGroupEntities.forEach(attrGroupEntity -> {
//            AttrGroupVo attrGroupVo = new AttrGroupVo();
//            BeanUtils.copyProperties(attrGroupEntity , attrGroupVo);//将正在遍历的分组的属性值设置给vo对象
//            //属性只查询spu的属性， sku的属性是销售属性(type=0)
//            List<AttrEntity> attrEntities = attrService.list(new LambdaQueryWrapper<AttrEntity>()
//                    .eq(AttrEntity::getGroupId, attrGroupEntity.getId()).eq(AttrEntity::getType,1));
//            attrGroupVo.setAttrEntities(attrEntities);
//            attrGroupVos.add(attrGroupVo);
//        });

//        return attrGroupVos;
    }

    @Override
    public List<ItemAttrGroupVo> queryItemAttrGroupsBySpuId(Long spuId) {

        return baseMapper.queryItemAttrGroupsBySpuId(spuId);
    }

}